function MembersRequest(url, remaining) {
    var actionsContainer = $('#members-list').next('.actions');
    actionsContainer.children('button').attr("disabled", "disabled");
    var availableMembersCount = parseInt($('#members_count').val());

    var currentPage = parseInt($('#members_current_page').val());
    var nextPage = currentPage + 1;

    var request = { page: nextPage }
    if (remaining)
        request['remaining'] = 1;

    $.ajax({
        type: "GET",
        url: url,
        data: request,
        dataType: "json",

        complete: function() {
            $('.loader-image').hide();
            actionsContainer.children('button').removeAttr("disabled");

            var visibilityMembersCount = $('#members-list .member').length
            if (visibilityMembersCount >= availableMembersCount)
                $(actionsContainer).remove();
        },
        success: function(response) {
            var template = $('#memberTemplate');
            var targetContainer = $('#members-list');

            $(response).each(function(i, data) {
                $(template).tmpl(data).appendTo(targetContainer);
            });

            $('#members_current_page').val(nextPage);
        },
        error: function() {
            alert("Error");
        }
    });
}

$(document).ready(function() {
    $('#more_members').live('click', function() { MembersRequest($(this).attr("data-url"), false) });
    $('#all_members').live('click', function() { MembersRequest($(this).attr("data-url"), true) });
});